package com.hzz.framework.util;

import com.hzz.dto.patrol.MapCordinatesDto;

import java.math.BigDecimal;

import static java.lang.Math.*;

public class CoordinatesConvertUtil {
    private static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    
    /**
     * 高德地图坐标转化为百度坐标
     * @param cordinatesVo
     */
    public static MapCordinatesDto bd_encrypt(MapCordinatesDto cordinatesVo)
    
    {
        if (cordinatesVo.getLat() == null || cordinatesVo.getLng() == null) {
            return cordinatesVo;
        }
        
        double x = cordinatesVo.getLng().doubleValue(), y = cordinatesVo.getLat().doubleValue();
        
        double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
        
        double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
        
        double bd_lon = z * cos(theta) + 0.0065;
        
        double bd_lat = z * sin(theta) + 0.006;
        
        MapCordinatesDto mapCordinatesVo = new MapCordinatesDto();
        BigDecimal lat = new BigDecimal(bd_lat);
        BigDecimal lon = new BigDecimal(bd_lon);
        
        mapCordinatesVo.setLat(lat.setScale(6, BigDecimal.ROUND_HALF_DOWN));
        mapCordinatesVo.setLng(lon.setScale(6, BigDecimal.ROUND_HALF_DOWN));
        
        return mapCordinatesVo;
    }
}
